//
//  UIViewController+CustemNavigationBarButtonItem.m
//  DianShangWen
//
//  Created by 周文轩 on 15/4/16.
//  Copyright (c) 2015年 &#21608;&#25991;&#36713;. All rights reserved.
//

#import "UIViewController+CustemNavigationBarButtonItem.h"

#import <objc/runtime.h>

@implementation UIViewController (CustemNavigationBarButtonItem)

+(void)load{
    Method viewWillAppear = class_getInstanceMethod(self, @selector(customViewWillAppear:));
    Method customViewWillAppear = class_getInstanceMethod(self, @selector(viewWillAppear:));
    method_exchangeImplementations(viewWillAppear, customViewWillAppear);
}

-(void)customViewWillAppear:(BOOL)animated{
    [self customViewWillAppear:animated];
    
    if ([self.navigationController.viewControllers indexOfObject:self] != 0 && !self.navigationItem.hidesBackButton) {
        self.parentViewController.tabBarController.tabBar.hidden = YES;
        [self constructLeftBarButton];
    }
}

-(void)constructLeftBarButton{
    
    UIBarButtonItem* leftBarButton = nil;
    
    UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setFrame:CGRectMake(0, 0, 44, 44)];
    [cancelButton setImage:[UIImage imageNamed:@"banck"] forState:UIControlStateNormal];
    [cancelButton addTarget:self
                     action:@selector(leftBarButtonAction)
           forControlEvents:UIControlEventTouchUpInside];
    
    leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
        
    [self.navigationItem setLeftBarButtonItem:leftBarButton];
}

-(void)leftBarButtonAction{
    self.parentViewController.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

@end
